package thrift.hello;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.thrift.TException;
import org.apache.thrift.TServiceClient;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import thrift.hello.HelloService.Iface;

import com.to8to.kitt.Constants;
import com.to8to.kitt.esb.client.ThriftClientPoolFactory;


public class HelloClient {
	
	public static String host = "192.168.1.100";
	public static int port = 30000;
	
//	public static String host = "192.168.3.147";
//	public static int port = 22222;
	
//	public static String host = "192.168.1.97";
//	public static int port = 30000;
	
	public static int timeout = 30000;
	
	public void testHello()
	{ 
		
//		HelloService.Iface iface = (HelloService.Iface)ThriftClientPoolFactory.getClient(HelloService.class);
		
		HelloService.Iface iface = null;
		
		try {
			iface = (Iface) ThriftClientPoolFactory.createClient(HelloService.class.getName(), host, port, timeout);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
//		while(true)
//		for(int j = 0 ; j < 100; ++ j)
		{
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				String res = iface.hello(System.nanoTime() + "");
				System.out.println(new Date() + "," + res);
			} catch (TException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public void testShortConn() throws Exception
	{
		final Logger LOGGER = LoggerFactory.getLogger(TSocket.class.getName());
		HelloService.Client ccc;
		TServiceClient xxxx; //TServiceClient  static{}初始化耗时90ms
		
		while(true)
		{
			long t1 = System.currentTimeMillis();
			
//			Socket socket_ = new Socket();
//			socket_.setSoLinger(false, 0);
//			socket_.setTcpNoDelay(true);
//			socket_.setSoTimeout(timeout);
//			socket_.connect(new InetSocketAddress(host, port), timeout);
//			InputStream inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024);
//			OutputStream outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024);

		      
			
			TTransport transport = new TSocket(host, port, timeout);
			
			long t2 = System.currentTimeMillis();
			
			transport = new TFramedTransport(transport, Constants.MAX_DATA_LENGTH);
			
			long t3 = System.currentTimeMillis();
			
			TProtocol protocol = new TBinaryProtocol(transport);
			
			long t4 = System.currentTimeMillis();
			
			HelloService.Client iface = new HelloService.Client(protocol);

			long t5 = System.currentTimeMillis();
			
			transport.open();
			
			long t6 = System.currentTimeMillis();
			
			System.out.println(iface.hello("xxxxx"));
			
			long t7 = System.currentTimeMillis();
			
			System.out.println("time1:" + (t2-t1));
			System.out.println("time2:" + (t3-t2));
			System.out.println("time3:" + (t4-t3));
			System.out.println("time4:" + (t5-t4));
			System.out.println("time5:" + (t6-t5));
			System.out.println("req time6:" + (t7-t6));
			
//			socket_.close();
			transport.close();
			
			Thread.sleep(1000);
		}
	}
	
//	
//	public void testStatus()
//	{
//		ThriftClient busStatusClient = new ThriftClient(BusStatusService.class.getName(), host, port, timeout);
//		
//		BusStatusService.Iface busiface = (BusStatusService.Iface)busStatusClient.getProxy(new HelloClientAdvice());
//		
//		List<ServiceStat> list = null;
//		
//		try {
//			list = busiface.getStat(null, null);
//		} catch (TException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		Gson gson = new Gson();
//		
//		System.out.println(gson.toJson(list));
//	}
//	
//	
	class MyRunnable implements Runnable
	{
		public HelloService.Iface iface;
		
		@Override
		public void run() {
			try {
				
				while(true)
				{
					{
						String tag = Thread.currentThread().getId() + "" + System.nanoTime();
						MDC.put("tag", tag);
						String res = iface.hello("hello");
						System.out.println(Thread.currentThread().getId() + ":" + res + ", tag:" + tag);
					}

					{
						
					}
					
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			} catch (TException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public void testMultiThread()
	{
		HelloService.Iface iface = (HelloService.Iface)ThriftClientPoolFactory.getClient(HelloService.class);
		
		MyRunnable r = new MyRunnable();
		
		r.iface = iface;
		
		ExecutorService pool = Executors.newCachedThreadPool();
		
		for(int i = 0; i < 10; ++ i)
		{
			pool.execute(r);
		}
		
		
		while(true)
		{
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 * @param args
	 * @throws TException 
	 */
	public static void main(String[] args) throws Exception {
		HelloClient c = new HelloClient();
		
		c.testShortConn();
		
//		c.testHello();
		
//		c.testStatus();
		
//		c.testMultiThread();
		
	}
}
